#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
检查快乐8历史数据文件
"""

import pandas as pd
import os

def check_kl8_data():
    """检查快乐8历史数据文件"""
    print("🔍 检查快乐8历史数据文件")
    print("=" * 50)
    
    try:
        # 文件路径
        data_file = os.path.join(os.path.dirname(__file__), 'scripts', 'kl8', 'kl8_history.csv')
        
        if not os.path.exists(data_file):
            print("❌ 数据文件不存在")
            return
            
        # 读取数据
        df = pd.read_csv(data_file, encoding='utf-8')
        print(f"✅ 成功加载数据，共 {len(df)} 期")
        print(f"📊 数据形状: {df.shape}")
        
        # 检查列数是否一致
        print("\n📋 检查列数一致性...")
        column_counts = df.count(axis=1)
        unique_counts = column_counts.unique()
        print(f"   列数变化: {sorted(unique_counts)}")
        
        if len(unique_counts) > 1:
            print("   ⚠️  发现列数不一致的行:")
            for count in unique_counts:
                rows = column_counts[column_counts == count]
                print(f"      {count} 列: {len(rows)} 行")
        else:
            print("   ✅ 所有行列数一致")
            
        # 检查特定行的数据
        print("\n📝 检查特定行数据...")
        target_row = 1708
        if len(df) > target_row:
            row_data = df.iloc[target_row]
            print(f"   第 {target_row+1} 行数据:")
            print(f"      {row_data.tolist()}")
            
            # 检查是否有重复的数字
            numbers = row_data[2:22].tolist()  # 取开奖号码列
            if len(numbers) == len(set(numbers)):
                print("   ✅ 号码无重复")
            else:
                print("   ❌ 发现重复号码")
                duplicates = [x for x in set(numbers) if numbers.count(x) > 1]
                print(f"      重复号码: {duplicates}")
        else:
            print(f"   ❌ 数据行数不足 {target_row+1} 行")
            
        # 检查最近几行数据
        print("\n📅 检查最近几行数据...")
        last_rows = df.tail(5)
        for i, (idx, row) in enumerate(last_rows.iterrows()):
            numbers = row[2:22].tolist()
            print(f"   第 {idx+1} 行: {numbers}")
            if len(numbers) != len(set(numbers)):
                print(f"      ❌ 第 {idx+1} 行存在重复号码")
            
    except Exception as e:
        print(f"❌ 检查失败: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    check_kl8_data()